<?php

class Admin_Model_MAuthor
{
    protected $connection=null;
    public function  __construct()
    {
       $this->connection=new Mongo();
    }

    public function addAuthor($name)
    {
        $db=$this->connection->bookstore;
        $collection=$db->author;
        $cursor=$collection->find(array(),array("_id"=>true,"name"=>true));
        foreach($cursor as $document)
        {
            if(strtolower($document["name"])==  strtolower($name))
            {
                return $document["_id"];
            }
        }

        $doAuthor=array("name"=>$name);
        $collection->insert($doAuthor);

        $doReturn=$collection->findOne($doAuthor);
        return $doReturn["_id"];
    }

    public function getAuthor($idAuthor)
    {
        $db=$this->connection->bookstore;
        $collection=$db->author;
        $id=new MongoId($idAuthor);
        return $collection->findOne(array("_id"=>$id));
    }
    public function getAuthors($fields=array())
    {
        $db=$this->connection->bookstore;
        $collection=$db->author;

        if( $fields == null || count($fields)==0)
            return $collection->find();
        else
        {
            $filter=array();
            for($i=0;$i<count($fields);$i++)
            {
               $filter[$fields[$i]]=true;
            }
            return $collection->find(array(),$filter);
        }

    }
}
?>
